package cn.skcks.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

import java.text.DateFormat;
import java.text.SimpleDateFormat;

public class JsonUtils {
	public static String getJson(Object object) {
		return getJson(object, "yyyy-MM-dd HH:mm:ss");
	}

	public static String getJson(Object object, String stringDateFormat) {
		ObjectMapper objectMapper = new ObjectMapper();

		objectMapper.configure(SerializationFeature.WRITE_DATE_KEYS_AS_TIMESTAMPS, false);

		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(stringDateFormat);

		objectMapper.setDateFormat(simpleDateFormat);

		try {
			return objectMapper.writeValueAsString(object);
		} catch (JsonProcessingException e) {
			e.printStackTrace();
			return null;
		}
	}
}